﻿@{
    ViewBag.Title = "PointExchange";
}
@section scripts
{
    <script src="@Url.Content("~/Scripts/shared/share.js")" type="text/javascript"></script> 
}

    <div class="mini-toolbar" style="padding:2px;border-top:0;border-left:0;border-right:0;"> 
        <table style="width:100%;">
            <tr>
                <td style="width:100%;">
                    <a class="mini-button" id="fresh" name="fresh" iconCls="icon-search" onclick="search" plain="true">刷新</a>
                    <a class="mini-button" id="new" name="new" iconCls="icon-new" onclick="ShowGoodsWin()" plain="true" visible="@ViewBag.add">新增</a>
                    <a class="mini-button" id="delrow" name="delrow" iconCls="icon-remove" onclick="removeRow" plain="true" visible="@ViewBag.remove">删除</a>
                    <a class="mini-button" id="save" name="save" iconCls="icon-edit" onclick="save" plain="true" visible="@ViewBag.save">保存</a>
                    <span class="separator"></span>
                    <a class="mini-button" id="close" name="close" iconCls="icon-close" onclick="CloseWindow" plain="true">关闭</a>
                </td>
            </tr>
        </table>       
    </div>
    <table>
        <tr>
            <td width ="100px" align="right">编码或名称: </td>
            <td width ="150px" ><input class="mini-textbox" width ="150px" id ="FSearchKey1" name = "FSearchKey1"  onkeypress="setkeyPress()"/></td>
            <td width ="100px" align="center">
                <a class="mini-button" iconCls="icon-search" onclick="search" plain="true">查询</a>
            </td>
        </tr>
    </table> 
    <div class="mini-fit" >
        <div id="datagrid1" class="mini-datagrid" style="width:100%;height:100%;" pageSize="15"
            borderStyle="border:1;" url="GetPointExchange"
            allowCellSelect="true" allowCellEdit="true"showColumnsMenu="true"fitColumns="false"  
            sortMode="client" multiSelect="true"editNextOnEnterKey="true"  editNextRowCell="false"
            skipReadOnlyCell="true" navEditMode="true"oncellcommitedit="onCellCommitEdit">
            <div property="columns">
                <div type="checkcolumn" ></div>
                <div type="indexcolumn" width="40" headerAlign="center" >序号</div>
                <div field="FNumber" name="FNumber" width="100" headeralign="center" allowsort="true">商品编码</div>                    
                <div field="FName" name="FName" width="150" headeralign="center" allowsort="true">商品名称</div>
                <div field="FSpec" name="FSpec" width="90" headeralign="center" allowsort="true">规格型号</div>                    
                <div field="FUnitName" name="FUnitName" width="40" headeralign="center" allowsort="true">单位</div> 
                <div field="FQty" name="FQty" width="80" headerAlign="center" align="right">兑换数量</div> 
                <div field="FPoint" name="FPoint" width="80" headerAlign="center" align="right">扣减积分
                    <input property="editor" class="mini-spinner" maxValue="99999999" style="width:100%;"changeOnMousewheel ="false" showbutton="false" increment ="0"/>
                </div>
                <div field="FIsUsed" type="checkboxcolumn" name="FIsUsed" trueValue="1" falseValue="0" width="60" headeralign="center" allowsort="true">是否启用</div> 
                <div field="FCode" name="FCode" width="150" headeralign="center" allowsort="true">商品条码</div>
            </div>
        </div>
    </div>
<script>
    $(function () {
        mini.parse();
        search();
    });
    function search() {
        var json = "[{";
        json = json + "'FNumber':" + "'" + mini.getbyName("FSearchKey1").getValue() + "',";
        json = json + "'mSystemAppID':'"+parent.window.getSystemAppID()+"'}]";
        var grid = mini.get("datagrid1");
        grid.load({ mjson: json });
        mini.get("save").setEnabled(false);
    }
    function setkeyPress() {
        if(event.keyCode==13)
            search();
    }

    function ShowGoodsWin() {
        mini.open({
            url: "../Bill/GetBillGoods",
            title: "选择商品", width: 900, height: 500,
            onload: function () {
                var iframe = this.getIFrameEl();
                var data = { key: "", mBillType: "0" };
                iframe.contentWindow.SetData(data);
            },
            ondestroy: function (action) {
                if (action == "close") return;
                if (action) {
                    var grid = mini.get("datagrid1");
                    for (var i = 0, l = action.length; i < l; i++) {
                        var rw = action[i];
                        var newRow = {
                            FGoodID: rw.FItemID, FNumber: rw.FNumber, FName: rw.FName, FSpec: rw.FSpec,
                            FUnitName: rw.FUnitName, FCode: rw.FCode, FQty: 1, FPoint: 1, FIsUsed: 1
                        };
                        grid.addRow(newRow, grid.getData().length);
                        mini.get("save").setEnabled(true);
                    }
                }
            }
        });
    }

    function removeRow() {
        var grid = mini.get("datagrid1");
        var rows = grid.getSelecteds();
        if (rows.length > 0) {
            grid.removeRows(rows, true);
            mini.get("save").setEnabled(true);
            mini.get("check").setEnabled(false);
        }
    }

    function save() {
        var grid = mini.get("datagrid1");
        var rows = grid.getChanges();
        if (rows.length == 0)
        {
            mini.get("save").setEnabled(false);
            return;
        }
        var json = mini.encode(rows);
        $.post("SavePointExchange", { json: json, mSystemAppID: parent.window.getSystemAppID() }, function (data) {
            var ret = mini.decode(data);
            if (ret.status == 1) {
                mini.get("save").setEnabled(false);
                mini.showTips({ content: ret.msg, state: "success", x: "center", y: "top", timeout: 3000 });
            }
            else {
                mini.alert(ret.error);
            }
            var grid = mini.get("datagrid1");
            grid.reload();
        });
    }


    function onCellCommitEdit(e) {
        mini.get("save").setEnabled(true);//编辑后保存按钮可用
    }
</script>
